"""
Case Type   : dolphin
Case Name   : 测试函数bit_count在latin1编码格式下的返回值
Create At   : 2022.12.05
Owner       : @zou_jialiang050
Description :
    1、创建编码格式为latin1的数据库;
    2、函数参数为数字
    3、函数参数为字母
    4、函数参数为汉字
    5、恢复环境
Expect      :
    1、创建数据库成功;
    2、成功
    3、成功
    4、成功
    5、恢复环境成功
History     :modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BitCount(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.constant = Constant()
        self.db_name = "db_bit_count_0014"

    def test_bit_count(self):
        step1_text = "-----step1:create database;expect:创建成功-----"
        self.logger.info(step1_text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
                    create database {self.db_name} dbcompatibility = 'B' 
                    encoding = 'latin1';'''
        self.logger.info(sql_cmd1)
        sql_res1 = self.commonsh.execut_db_sql(sql_cmd1)
        self.logger.info(sql_res1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res1,
                      "创建数据库失败:" + step1_text)

        step2_text = "-----step2:函数参数为数字;expect:查询成功-----"
        self.logger.info(step2_text)
        sql_cmd2 = f'''select bit_count(123);'''
        self.logger.info(sql_cmd2)
        sql_res2 = self.commonsh.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.logger.info(sql_res2)
        msg = "bit_count \n-----------\n         6\n(1 row)"
        self.assertIn(msg, sql_res2, "查询失败：" + step2_text)

        step3_text = "-----step3:函数参数为字母;expect:查询成功-----"
        self.logger.info(step3_text)
        sql_cmd3 = f'''select bit_count('a');'''
        self.logger.info(sql_cmd2)
        sql_res3 = self.commonsh.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.logger.info(sql_res3)
        self.assertIn('0', sql_res3, "查询失败：" + step3_text)

        step4_text = "-----step4:函数参数为汉字;expect:查询成功-----"
        self.logger.info(step4_text)
        sql_cmd4 = f'''select bit_count('哈哈哈');'''
        self.logger.info(sql_cmd2)
        sql_res4 = self.commonsh.execut_db_sql(sql_cmd4, dbname=self.db_name)
        self.logger.info(sql_res4)
        self.assertIn('0', sql_res4, "查询失败：" + step4_text)

    def tearDown(self):
        step5_text = "-----step5:清理环境;expect:成功-----"
        self.logger.info(step5_text)

        drop_db_text = "-----drop database-----"
        self.logger.info(drop_db_text)
        drop_db_cmd = f'''drop database if exists {self.db_name};'''
        self.logger.info(drop_db_cmd)
        drop_db_res = self.commonsh.execut_db_sql(drop_db_cmd)
        self.logger.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      drop_db_res, "drop database filed" + drop_db_res)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
